<template>
  <el-card>
    <template #header>
      <h3>商品分类</h3>
    </template>

    <div>
      <el-button size="mini" type="primary" @click="dialogVisible = true">
        添加分类
      </el-button>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%" size="mini">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="分类名称">
          <template v-slot="{ row }">
            <span v-if="row.flag">{{ row.cateName }} </span>
            <el-input v-else v-model="row.cateName" size="mini"> </el-input>
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="row.flag"
            >
              >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              :type="row.flag ? 'primary' : 'success'"
              @click="editCate(row)"
              >{{ row.flag ? '编辑' : '完成' }}</el-button
            >
            <el-button size="mini" type="danger" @click="delCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 添加的面板 -->
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="40%">
        <div>
          <el-form
            :model="cateForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
            size="mini"
          >
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="cateForm.name"></el-input>
            </el-form-item>
            <el-form-item label="是否启用" prop="state">
              <el-switch
                v-model="cateForm.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-form>
        </div>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini"
            >取 消</el-button
          >
          <el-button type="primary" size="mini" @click="addCate('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import {
  goodsCate,
  AddgoodsCate,
  delgoodsCate,
  editgoodsCate
} from '@/api/goods.js'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      dialogVisible: false,
      cateForm: {
        name: '',
        state: true
      },
      rules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getGoodsCate()
  },
  methods: {
    async getGoodsCate() {
      const { data, total } = await goodsCate({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      // 给每一条数据增加一条属性
      data.forEach(item => {
        item.flag = true
      })
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getGoodsCate()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getGoodsCate()
    },
    addCate(str) {
      this.$refs[str].validate(async valid => {
        if (valid) {
          const { code, msg } = await AddgoodsCate({
            cateName: this.cateForm.name,
            state: this.cateForm.state
          })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
            this.dialogVisible = false
            this.getGoodsCate()
          }
        } else {
          this.$message.error('表单不能为空')
        }
      })
    },
    delCate(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await delgoodsCate({ id })
          if (data.code === 0) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            this.getGoodsCate()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async editCate(row) {
      if (row.flag) {
        // 修改分类
        row.flag = !row.flag
      } else {
        // 发请求
        const { code, msg } = await editgoodsCate({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state)
        })
        if (code === 0) {
          this.$message({
            type: 'success',
            message: msg
          })
          this.getGoodsCate()
        }
        row.flag = !row.flag
      }
    }
  },
  watch: {
    dialogVisible() {
      if (!this.dialogVisible) {
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
